//
//  NetworkTools.swift
//  WeiBo
//
//  Created by tusm on 2017/6/22.
//  Copyright © 2017年 GM. All rights reserved.
//

import UIKit
import AFNetworking

//网络请求方法
enum RequestMetod:String {
    case Get = "Get"
    case Post = "Post"
}

class NetworkTools: AFHTTPSessionManager {
    
     let appKey = "1109650105"
    
     let appSecret = "17cace5681dc5febe8062b60ad936e09"
    
     let appRedirectURI = "https://www.baidu.com"
    
     typealias RequestCallBack = (_ result:AnyObject?, _ error:NSError?) -> ()
    
    //单列
    static let shareTools:NetworkTools = {
        
        let tools = NetworkTools(baseURL: nil)
        
        //设置反序列化格式
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
        
    }()
    
   

}

extension NetworkTools{
    
    var oauthURL:URL{
        
        let stringURL = "https://api.weibo.com/oauth2/authorize?client_id=\(appKey)&redirect_uri=\(appRedirectURI)"
        
        return URL(string: stringURL)!
    }
    
    
    
    func loadAcceaaToken(code:String,finished: @escaping RequestCallBack){
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let params = ["client_id":appKey,
                      "client_secret":appSecret,
                      "grant_type":"authorization_code",
                      "code":code,
                      "redirect_uri":appRedirectURI]
        
        request(method: .Post, URLString: urlString, parameters: params as [String : AnyObject]?, progress: nil,finished:finished)
        
        
//        //MARK:测试验证数据格式代码
//        responseSerializer = AFHTTPResponseSerializer()
//        
//        post(urlString, parameters: params, success: { (_, result) in
//            
//            let json = NSString(data: (result as! NSData) as Data, encoding: String.Encoding.utf8.rawValue)
//            
//            print(json as Any)
//            
//        }, failure: nil)
//        
//        //原始数据格式：
//        //"access_token":"2.00OAbgGG2gyFNB263e0837d7ZKvfbC",
//        //"remind_in":"157679999",
//        //"expires_in":157679999, ---NSNumber
//        //"uid":"5595607026"
    }
}


// MARK: - 用户信息加载请求
extension NetworkTools{
    
    func loadUserInfo(accessToken:String, uid:String, finished:@escaping RequestCallBack){
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        let parames = ["access_token":accessToken,
                       "uid":uid]
        
        request(method: .Get, URLString: urlString, parameters: parames as [String : AnyObject]?, progress: nil, finished: finished)
    }
    
}

// MARK: - 网络请求封装
extension NetworkTools{
    
    func request(method:RequestMetod, URLString: String, parameters: [String:AnyObject]?, progress: Progress?, finished: @escaping RequestCallBack ){
        
        //定义成功回调
        let success = { (task:URLSessionDataTask, result:Any?) in
            
            finished(result as AnyObject?, nil)
            
        }
        
        //定义失败回调
        let failure = { (task:URLSessionDataTask?, error:Error) in
            
            
            print(error)
            
            finished(nil, error as NSError?)
            
        }
        
        //判断使用请求方法
        if method == .Get{
            
            get(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
        } else{
            
            post(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
        }
        
        
        
//        if method == .Get{
//        get(URLString, parameters: parameters, progress: nil, success: { (task:URLSessionDataTask, result:Any?) in
//        
//              finished(result as AnyObject?, nil)
//        
//        }) { (task:URLSessionDataTask?, error:Error) in
//        
//    
//              print(error)
//        
//              finished(nil, error as NSError?)
//        
//        }
//        } else{
//            
//
//        post(URLString, parameters: parameters, progress: nil, success: { (task:URLSessionDataTask, result:Any?) in
//            
//            finished(result as AnyObject?, nil)
//            
//        }) { (task:URLSessionDataTask?, error:Error) in
//            
//            
//            print(error)
//            
//            finished(nil, error as NSError?)
//            
//        }
//        
//     }
    }
}
